<?php 
## include required files
/*******************************/
require_once '../model/admindetails.php';
/*******************************/
## Create Objects
/*******************************/
$adminObj = new Model_AdminDetails();
/*******************************/
##profile image dimension message
$smarty->assign('profileimage_dimension', PROFILEIMAGE_WIDTHHEIGHT);

$userId=$_SESSION["admin_id"];
## fetching admin information
$adminArray=$adminObj->getAdminUserDetailsByUserId($userId);	
$smarty->assign('adminArray', $adminArray);

if(isset($_POST['editprofile_btn'])) {
	## apply PHP validation for required filed
	if(trim($_POST['first_name']) != '' && trim($_POST['last_name']) != '') {
			extract($_POST);
			$userArray = array();
			$userArray['first_name']  = return_post_value($first_name);
			$userArray['last_name']  = return_post_value($last_name);
			$adminObj->editUserValueById($userArray,$userId);
			if($_FILES['user_avatar']['name']!='') {
				
				$size =	getimagesize($_FILES['user_avatar']['tmp_name']);
				if($_FILES['user_avatar']['name']!= '' && ($size[0]>=PROFILEIMAGEWIDTH && $size[1]>=PROFILEIMAGEHEIGHT) ) {
					
					##unlink the prevoius logo
				$user_image = '../uploads/user/'.$adminArray['user_avatar']; 
				@unlink($user_image);
				
				$path = '../uploads/user/';
				$imageName    = $_FILES['user_avatar']['name'];
				$arrImageName = explode(".",$imageName);
				$position     = count($arrImageName);
				$fileName     = date('Ymdhis').".".$arrImageName[$position-1];
				copy($_FILES["user_avatar"]["tmp_name"],$path.$fileName);
				$imageArray=array();
				$imageArray['user_avatar']=$fileName;
				$adminObj->editUserValueById($imageArray,$_SESSION["admin_id"]);
				} else {
					$_SESSION['msg'] = "<div class='error_msg'><span>".PROFILEIMAGE_WIDTHHEIGHT."</span></div>";
					header('location:'.SITE_URL.'/admin/home.php?q=editprofile');
			exit;
				}	
			}
			$_SESSION['msg'] = "<div class='success_msg'><span>You have edited profile information successfully.</span></div>";
			header('location:'.SITE_URL.'/admin/home.php');
			exit;
	} else {// php validation else
		$_SESSION['msg'] = "<div class='error_msg'><span>Please enter first and last name.</span></div>";
	}
	// main if closed
}

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Unset all the objects created which are on this page
unset($adminObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'settings/editprofile.tpl');
unset($smarty);
?>